<?php

declare(strict_types=1);

namespace App\Service;

use App\Constants\ErrorCode;
use App\Dao\OilAmountDao;
use App\Dao\RechargeAmountDao;
use App\Dao\RechargeTypeDao;
use App\Dao\RechargeAmountChannelDao;
use App\Exception\BusinessException;
use App\Utils\ServerUtil;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use App\Dao\AdminLogDao;
class AmountService
{

   /**
    * @Inject
    * @var AdminLogDao
    */
   protected $adminLogLogic;

   /**
    * @Inject
    * @var OilAmountDao
    */
   protected $oilAmountDao;

   /**
    * @Inject
    * @var RechargeAmountDao
    */
   protected $rechargeAmountDao;

   /**
    * @Inject
    * @var RechargeTypeDao
    */
   protected $rechargeTypeDao;

   /**
    * @Inject
    * @var RechargeAmountChannelDao
    */
    protected $RechargeAmountChannelDao;
   

   //根据条件获取油卡费用分页列表
   public function oilList($params)
   {
      return $this->oilAmountDao->pageByWhere([], ['page' => $params['page'], 'pageSize' => $params['pageSize']]);
   }

   public function oil($params){
      return $this->oilAmountDao->getIndexByWhere($params['where'],$params['fields'],[],$params['orderBy']);
   }


   //新增油卡费用
   public function oilAdd($params)
   {  
      if ($this->oilAmountDao->isLegalAmount($params['ori_amount'], $params['type_id'])) {
         throw new BusinessException(ErrorCode::ERR_BUESSUS, '失败！已存在对应价格类型规格' );
      }
      try {
         $params['amount'] =  ($params['ori_amount'] * $params['discount'])/10;
         $res = $this->oilAmountDao->addTrait($params);
         $this->adminLogLogic->addLog('新增油卡费用' . $res->toJson(JSON_UNESCAPED_UNICODE));
      } catch (\Throwable $t) {
         // var_dump($t->getMessage());
         ServerUtil::info('新增油卡费用异常', ['msg' => $t->getMessage()]);
         throw new BusinessException(ErrorCode::ERR_BUESSUS, '新增油卡费用异常');
      }
   }

   //编辑油卡费用
   public function oilEdit($params)
   {

      if ($this->oilAmountDao->isLegalAmount($params['ori_amount'], $params['type_id']) && $this->oilAmountDao->isLegalAmount($params['ori_amount'], $params['type_id']) != $params['id']) {
         throw new BusinessException(ErrorCode::ERR_BUESSUS, '失败！已存在对应价格类型规格' );
      }
      try {
         $logInfo = $this->oilAmountDao->findTrait($params['id'])->toJson(JSON_UNESCAPED_UNICODE);
         $params['amount'] = ($params['ori_amount'] * $params['discount'])/10;
         $this->oilAmountDao->updateTrait($params);
         $this->adminLogLogic->addLog('编辑油卡费用,编辑前' . $logInfo . '编辑后' . $this->oilAmountDao->findTrait($params['id'])->toJson(JSON_UNESCAPED_UNICODE));
      } catch (\Throwable $t) {
         // var_dump($t->getMessage());
         ServerUtil::info('编辑油卡费用异常', ['msg' => $t->getMessage()]);
         throw new BusinessException(ErrorCode::ERR_BUESSUS, '编辑油卡费用操作异常');
      }
   }

   //删除油卡费用金额
   public function oilDel($params)
   {
      $logInfo = $this->oilAmountDao->findTrait($params['id'])->toJson(JSON_UNESCAPED_UNICODE);
      $params['is_del'] = 2;
      $this->oilAmountDao->updateTrait($params);
      $this->adminLogLogic->addLog('删除油卡费用金额' . $logInfo);
   }

   //编辑油卡费用渠道
   public function oilChannel($params)
   {
      $this->adminLogLogic->addLog('编辑油卡费用渠道关联' . json_encode($params, JSON_UNESCAPED_UNICODE));
      //更改渠道状态
      return $this->oilAmountDao->updateChannelStatusById($params['id']);
   }

   //话费金额类型
   public function typeList()
   {
      return $this->rechargeTypeDao->getByWhere();
   }

   //话费金额新增
   public function typeAdd($params)
   {  
      
      Db::beginTransaction();
      $res = $this->rechargeTypeDao->addTrait($params);
      if ($res->id == 1 && strpos($res->name, '快充') === false) {
         Db::rollback();
         throw new BusinessException(ErrorCode::ERR_BUESSUS, '操作失败,基础数据限制id：1昵称必须包含快充');
      }

      if ($res->id == 2 && strpos($res->name, '慢充') === false) {
         Db::rollback();
         throw new BusinessException(ErrorCode::ERR_BUESSUS, '操作失败,基础数据限制id：2昵称必须包含慢充');
      }
      Db::commit();
      //添加管理员操作日记
      $this->adminLogLogic->addLog('新增充值类型' . $res->toJson(JSON_UNESCAPED_UNICODE));
   }

   public function typeEdit($params)
   {  
      $logInfo = $this->rechargeTypeDao->findTrait($params['id'])->toJson(JSON_UNESCAPED_UNICODE);
      $this->rechargeTypeDao->updateTrait($params);
      //添加管理员操作日记
      $this->adminLogLogic->addLog('编辑了充值类型' . $logInfo . '编辑成功后' . $this->rechargeTypeDao->findTrait($params['id'])->toJson(JSON_UNESCAPED_UNICODE));
   }
   
   //充值金额类型删除
   public function typeDel($params)
   {
      if ($this->rechargeAmountDao->existsByWhereTrait(['type_id'=>$params['id']])) {
         throw new BusinessException(ErrorCode::ERR_BUESSUS, '充值金额存在关联无法删除');
      }
      $logInfo = $this->rechargeTypeDao->findTrait($params['id'])->toJson(JSON_UNESCAPED_UNICODE);
      $params['is_del'] = 2;
      $this->rechargeTypeDao->updateTrait($params);
      $this->adminLogLogic->addLog('删除油卡费用金额' . $logInfo);
   }

   //充值金额列表信息
   public function amountList(){
      return $this->rechargeAmountDao->getAll();
   }

   //充值金额列表信息
   public function amount($params){
      return $this->rechargeAmountDao->getByWhere($params['where'],$params['fields'],[],$params['orderBy']);
   }

   //充值金额新增
   public function amountAdd($params)
   {  
      if ($this->rechargeAmountDao->isLegalAmount($params['ori_amount'], $params['type_id'])) {
          throw new BusinessException(ErrorCode::ERR_BUESSUS, '失败！已存在对应价格类型规格' );
      }
      $res=$this->rechargeAmountDao->addTrait($params);
      $this->adminLogLogic->addLog('添加了充值面额' . $res->toJson(JSON_UNESCAPED_UNICODE));
   }

   //充值金额编辑
   public function amountEdit($params){
      if ($this->rechargeAmountDao->isLegalAmount($params['ori_amount'], $params['type_id']) && $this->rechargeAmountDao->isLegalAmount($params['ori_amount'], $params['type_id']) != $params['id']) {
         throw new BusinessException(ErrorCode::ERR_BUESSUS, '失败！已存在对应价格类型规格' );
      }
      $logInfo = $this->rechargeAmountDao->findTrait($params['id'])->toJson(JSON_UNESCAPED_UNICODE);
      $this->rechargeAmountDao->updateTrait($params);
      //添加管理员操作日记
      $this->adminLogLogic->addLog('编辑了充值面额' . $logInfo . '编辑后' . $this->rechargeAmountDao->findTrait($params['id'])->toJson(JSON_UNESCAPED_UNICODE));
   }

   //删除金额
   public function amountDel($params){
      $logInfo = $this->rechargeAmountDao->findTrait($params['id'])->toJson(JSON_UNESCAPED_UNICODE);
      $params['is_del'] = 2;
      $this->rechargeAmountDao->updateTrait($params);
      $this->adminLogLogic->addLog('删除了充值面额' . $logInfo);
   }

   //编辑渠道关联
   public function channelEdit($params){
      $info=$this->RechargeAmountChannelDao->findTrait($params['id']);
      $status=$info->status==1?2:1;
      //添加管理员操作日记
      $logInfo = $info->toJson(JSON_UNESCAPED_UNICODE);
      $this->RechargeAmountChannelDao->update(['id'=>$params['id']],['status'=>$status]);
      //添加管理员操作日记
      $this->adminLogLogic->addLog('编辑充值金额--金额渠道：编辑前 ' . $logInfo . '编辑后' . json_encode($this->RechargeAmountChannelDao->searchByWhere(['id'=>$params['id']]),JSON_UNESCAPED_UNICODE));
   }

   /**
     * 判断话费金额是否合法,存在返回正常状态的 -id
     * @Author czm
     * @DateTime 2020-04-20
     * @param [type] $ori_amount 初始金额
     * @param [type] $amount 售价金额
     * @param [type] $type_id  1快充 2慢充  
     * @return mixed
     */
   public function isLegalDisCountAmount($ori_amount, $amount, $type_id){
      return  $this->rechargeAmountDao->isLegalDisCountAmount($ori_amount,$amount, $type_id);
   }

   /**
     * 判断油卡金额是否合法,存在返回正常状态的 -id
     * @Author czm
     * @DateTime 2020-04-20
     * @param [type] $ori_amount 初始金额
     * @param [type] $amount 售价金额
     * @param [type] $type_id  1中石油 2中石化 
     * @return mixed
     */
    public function isOilLegalDisCountAmount($ori_amount, $amount, $type_id){
      return  $this->oilAmountDao->isLegalDisCountAmount($ori_amount,$amount, $type_id);
   }

   /**
     * 返回可以状态话费充值类型数据
     * 
     */
    public function rechargeType($params)
    {
       return $this->rechargeTypeDao->getByWhere($params);
    }
   

}
